<template>
  <div>
    <s-table
      ref="table"
      class="user_management_table"
      :columns="columns"
      :data="loadData"
      
    >
      <span slot="created_at" slot-scope="text">{{text|formatDate}}</span>
      <span slot="status" slot-scope="text, record">
        <span v-if="record.status==0">未下载</span>
        <span v-if="record.status==1">已下载</span>
      </span>
      <!-- <span slot="file_size"  slot-scope="text, record">
       {{text}}kb
      </span>-->
      <!-- <span slot="status" slot-scope="text, record" v-if="record.status==1">
      已下载
      </span>-->
      <span slot="action" slot-scope="text, record">
        <!-- <a-button
          type="primary"
          @click="excelOut(record.id,record.file_name)"
          style="margin-right:10px"
        >下载</a-button>-->
        <a @click="excelOut(record.id,record.file_name)">下载</a>
        <a-divider type="vertical" />
        <a @click="del(record.id)">删除</a>
        <!-- <a-button type="danger" @click="del(record.id)">删除</a-button> -->
      </span>
    </s-table>
  </div>
</template>

<script>
import { STable } from '@/components'
import { exportList, takeClassExcel, deleteExport } from '@/api/outgoingfile'
export default {
  name: 'importfile',
  data() {
    return {
      name: 'importfile',
      columns: [
        {
          title: '文件名',
          dataIndex: 'file_name',
          width:500,
        },
        {
          title: '时间',
          dataIndex: 'created_at',
          align: 'center',
          scopedSlots: { customRender: 'created_at' }
        },

        {
          title: '文件大小',
          dataIndex: 'file_size',
          align: 'center',
          customRender(file_size) {
            return file_size + 'KB'
          }
        },
        {
          title: '状态',
          dataIndex: 'status',
          align: 'center',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      queryParam: {
        school_id: this.$store.state.user.school
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return exportList(Object.assign(parameter, this.queryParam)).then(res => {
          return res
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },

  components: {
    STable
  },
  created() {},
  methods: {
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    excelOut(id, name) {
      takeClassExcel(id).then(res => {
        let url = res.url
        window.location.href = url
        // console.log(res)
        // this.download(blobUrl, name)
      })
    },

    download(blobUrl, name) {
      const a = document.createElement('a')
      a.style.display = 'none'
      a.download = name
      a.href = blobUrl
      a.click()
      document.body.removeChild(a)
    },
    del(id) {
      let that = this
      this.$confirm({
        title: '提示',
        content: '确认删除?',
        okText: '确认',
        okType: 'primary',
        cancelText: '取消',
        onOk() {
          deleteExport(id)
            .then(res => {
              that.$refs.table.refresh()
              that.$message.success('删除成功')
            })
            .catch(err => {
              that.$message.error(err.data.message || '删除失败')
            })
        },
        onCancel() {}
      })
    }
  }
}
</script>

<style>
</style>